
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Diagnostics;
using System.Xml;

using Swaf;
using Swaf.Config;

namespace Swaf.Gui
{
	/// <summary>
	/// Summary description for GuiManager.
	/// </summary>
	public abstract class GuiManager : IGuiManager, IConfigPartner
	{
		//protected IApplication m_app;
		protected static IClassFactory s_widgetFactory=null;
		protected static IClassFactory s_bpFactory=null;
		protected IApplication m_app
		{
			get 
			{
				return Application.currentApp;
			}
			set{}
		}
		public GuiManager(IApplication app)
		{
			m_app = app;
		}

		public static IClassFactory WidgetFactory
		{
			get
			{
				if(s_widgetFactory == null)
				{
					s_widgetFactory = Application.currentApp.factories["GuiWidgets"];
					Debug.Assert(s_widgetFactory != null);
				}
				return s_widgetFactory;
			}
		}

		public static IClassFactory BusinessProcessFactory
		{
			get
			{
				if(s_bpFactory == null)
				{
					s_bpFactory = Application.currentApp.factories["GuiBusinessProcesses"];
					Debug.Assert(s_bpFactory != null);
				}
				return s_bpFactory;
			}
		}

		public virtual IGuiWidget createWidget(string name)
		{
			return (IGuiWidget)WidgetFactory.createFromNickName(name);
		}

		public virtual IGuiWidget createWidget(XmlElement createInfo)
		{
			IGuiWidget widget = (IGuiWidget)WidgetFactory.createFromNickName(createInfo.Name);
			if(widget is ILayoutTemplateHandler)
				((ILayoutTemplateHandler)widget).LayoutTemplate = createInfo;
			return widget;
		}

		public virtual IBusinessProcess createBusinessProcess(string name, IView view)
		{
			return (IBusinessProcess)BusinessProcessFactory.createFromNickName(name, m_app, view, null);
		}

		public virtual IBusinessProcess createBusinessProcess(XmlElement createInfo, IView view)
		{
			IBusinessProcess proc = null;

			proc = (IBusinessProcess)BusinessProcessFactory.createFromNickName(createInfo.Name, m_app, view, createInfo);

			return proc;
		}

		public void registerForConfig(IConfigManager config)
		{
			s_bpFactory = null;
			s_widgetFactory = null;
		}

		public void checkConfigStatus()
		{
		}

		public virtual IView simpleCreateView(string viewName, object viewHome)
		{
			return createView(viewName, viewHome, null);
		}

		public abstract ILocation createLocationForGuiType(IGuiWidget widget, string name, object guiLocation, object helper);
		public abstract IView createView(string viewName, object viewHome, IViewHandler handler, params IGuiElement[] namedItems);
	}
}
